
//
//  WBNetworkingManager+Extension.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/9/28.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import Foundation


extension WBNetworkingManager {
    
    /// 
    ///
    /// - parameter completion: <#completion description#>
    func statiList(since_id: Int64 = 0, max_id: Int64 = 0, completion:@escaping (_ statusList: [[String: Any]], _ isSuccess: Bool) -> ())   {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["since_id": since_id, "max_id": max_id > 0 ? max_id - 1 : 0]
        
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            let jsonArray = json as? [String: Any]
            
            let list =  jsonArray?["statuses"] as? [[String: Any]]
            
            completion(list ?? [], isSuccess)
            
        }
        
    }
    
    func unreadCount(completion: @escaping (_ count: Int) -> ()) {
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        guard  let uid = userAccount.uid else {
            
            completion(0)
            
            return
        }
        
        let params = ["uid": uid]
        
        request(URLString: urlString, parameters: params) { (result, isSuccess) in
            
            let unreadCountDict = result as! [String: Any]
            
            let ureadCount = unreadCountDict["status"] as! Int
            
            completion(ureadCount)
        }
        
    }
    
    //加载用户信息
    func loadUserInfo(completion: @escaping (_ dict: [String: Any]) -> ())  {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        guard let uid = userAccount.uid else {
            return
        }
        
        
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (result, isSuccess) in
            
            completion((result as? [String:Any]) ?? [:])
            
        }
    }
    
    func loadAccessToken(code: String,completion:@escaping (_ isSuccess: Bool)->()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":AppKey,
                      "client_secret":AppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":RedirectURI]
        
        
        
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            
            print("loadAccessToken === result====\(json)")
            
            self.userAccount.yy_modelSet(with: json as? [String: Any] ?? [:])
            
            
            self.loadUserInfo(){ (result) in
                
                self.userAccount.yy_modelSet(with: result)
                
                print(self.userAccount)
                
                self.userAccount.saveAccount()
                
                completion(isSuccess)
            }
        }
    }
}


